<?php

namespace app\storeapi\controller\shop;

use app\storeapi\controller\Controller;
use app\storeapi\model\store\Shop as ShopModel;
use app\storeapi\model\store\shop\Clerk as ClerkModel;

/**
 * 门店店员控制器
 * Class Clerk
 * @package app\store\controller\shop
 */
class Clerk extends Controller
{
    /**
     * 店员列表
     * @param int $shop_id
     * @param string $search
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function index()
    {
        $param = $this->postData();
        // 店员列表
        $model = new ClerkModel;
        $list = $model->getList($param);
        return $this->renderSuccess($list);

    }

    /**
     * 添加店员
     * @return array|bool|mixed
     * @throws \Exception
     */
    public function add()
    {
        $param = $this->postData();
        $model = new ClerkModel;
        // 新增记录
        if ($model->add($param)) {
            return $this->renderSuccess([],'添加成功');
        }
        return $this->renderError($model->getError() ?: '添加失败');
    }

    /**
     * 店员详情
     * @return array|bool|mixed
     * @throws \Exception
     */
    public function detail()
    {
        $param = $this->postData();
        $model = ClerkModel::detail($param['clerk_id']);
        if(!$model)  return $this->renderError('该代言人不存在，请刷新后重试');
        return $this->renderSuccess($model);
    }



    /**
     * 编辑店员
     * @param $clerk_id
     * @return array|bool|mixed
     * @throws \think\exception\DbException
     */
    public function edit()
    {
        $param = $this->postData();
        // 店员详情
        $model = ClerkModel::detail($param['clerk_id']);
        if(!$model)  return $this->renderError('该代言人不存在，请刷新后重试');
        if ($model->edit($param)) {
            return $this->renderSuccess([],'更新成功');
        }
        return $this->renderError($model->getError() ?: '更新失败');
    }

    /**
     * 删除店员
     * @param $clerk_id
     * @return array
     * @throws \think\exception\DbException
     */
    public function delete()
    {
        $param = $this->postData();
        // 店员详情
        $model = ClerkModel::detail($param['clerk_id']);
        if(!$model)  return $this->renderError('该代言人不存在，请刷新后重试');
        if (!$model->setDelete()) {
            return $this->renderError($model->getError() ?: '删除失败');
        }
        return $this->renderSuccess([],'删除成功');
    }

}